package com.example.myapplication0523.activity;

import static android.widget.Toast.LENGTH_SHORT;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication0523.R;

public class SMSActivity extends Activity implements View.OnClickListener {
    private EditText accountNameEt;
    private EditText smsCodeEt;
    private Button smsCodeBtn;
    private Button loginBtn;
    private CountDownTimer countDownTimer;
    private static final long COUNTDOWN_TIME = 60000;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        initView(); //配置组件
    }

    /**
     * 配置组件
     */
    private void initView() {
        accountNameEt= findViewById(R.id.et_accountName);
        smsCodeEt= findViewById(R.id.et_smsCode);
        smsCodeBtn= findViewById(R.id.btn_smsCode);
        loginBtn= findViewById(R.id.btn_login);
        smsCodeBtn.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_smsCode: //获取验证码
                if (TextUtils.isEmpty(accountNameEt.getText().toString())){
                    Toast.makeText(this, "账号不能为空", LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this, "验证码已发送，请注意查收", LENGTH_SHORT).show();
                startCountdown();
                break;
            case R.id.btn_login: //登录
                if(TextUtils.isEmpty(accountNameEt.getText().toString())){
                    Toast.makeText(this, "账号不能为空", LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(smsCodeEt.getText().toString())){
                    Toast.makeText(this, "密码不能为空", LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this, "账号"+ accountNameEt.getText()+ "，验证码"+ smsCodeEt.getText()+ "，执行登录", LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 倒计时
     */
    private void startCountdown() {
        countDownTimer = new CountDownTimer(COUNTDOWN_TIME, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // 更新UI，显示剩余时间
                smsCodeBtn.setText("剩余时间: " + millisUntilFinished / 1000 + " 秒");
                smsCodeBtn.setEnabled(false);
            }

            @Override
            public void onFinish() {
                // 倒计时结束后的逻辑，如显示提示信息
                smsCodeBtn.setText("获取验证码");
                smsCodeBtn.setEnabled(true);
            }
        };
        countDownTimer.start();
    }
}
